import streamlit as st

class EmailSettingsView:
    """邮箱设置视图类"""
    
    def __init__(self):
        pass
    
    def render(self):
        """渲染邮箱设置页面"""
        st.markdown("<h1 class='main-header'>邮箱设置</h1>", unsafe_allow_html=True)
        
        with st.form("email_settings_form"):
            # 基础配置
            st.subheader("基础配置")
            col1, col2 = st.columns(2)
            with col1:
                email_folder_version = st.text_input("邮件文件夹版本", value=st.session_state.config_params.get('email_folder_version', 'V12'))
                max_email_count = st.number_input("最大邮件数量", value=st.session_state.config_params.get('max_email_count', 1), min_value=-1)
            with col2:
                max_retries = st.number_input("最大重试次数", value=st.session_state.config_params.get('max_retries', 5), min_value=1)
                email_folder = st.text_input("邮件文件夹", value=st.session_state.config_params.get('email_folder', 'INBOX'))
            
            # IMAP配置
            st.subheader("IMAP设置")
            col1, col2 = st.columns(2)
            with col1:
                imap_username = st.text_input("IMAP用户名", value=st.session_state.config_params.get('imap_username', ''))
                imap_password = st.text_input("IMAP密码", type="password", value=st.session_state.config_params.get('imap_password', ''))
            with col2:
                imap_server = st.text_input("IMAP服务器", value=st.session_state.config_params.get('imap_server', ''))
                imap_port = st.number_input("IMAP端口", value=st.session_state.config_params.get('imap_port', 993))
            
            # SMTP配置
            st.subheader("SMTP设置")
            col1, col2 = st.columns(2)
            with col1:
                smtp_username = st.text_input("SMTP用户名", value=st.session_state.config_params.get('smtp_username', ''))
                smtp_password = st.text_input("SMTP密码", type="password", value=st.session_state.config_params.get('smtp_password', ''))
            with col2:
                smtp_server = st.text_input("SMTP服务器", value=st.session_state.config_params.get('smtp_server', ''))
                smtp_port = st.number_input("SMTP端口", value=st.session_state.config_params.get('smtp_port', 6666))
            
            # 其他设置
            st.subheader("其他设置")
            max_threads = st.number_input("最大并发线程数", value=st.session_state.config_params.get('max_threads', 3), min_value=1)
            heartbeat_interval = st.number_input("心跳间隔", value=st.session_state.config_params.get('heartbeat_interval', 20), min_value=1)
            processed_uids_max_size = st.number_input("已处理邮件缓存大小", value=st.session_state.config_params.get('processed_uids_max_size', 10), min_value=1)
            max_results_memory_limit = st.number_input("最大结果内存限制", value=st.session_state.config_params.get('max_results_memory_limit', 10), min_value=1)
            max_deleted_email_limit = st.number_input("最大删除邮件限制", value=st.session_state.config_params.get('max_deleted_email_limit', 10), min_value=1)

            submitted = st.form_submit_button("保存邮箱设置", use_container_width=True)
            
            if submitted:
                st.session_state.config_params.update({
                    'email_folder_version': email_folder_version,
                    'max_email_count': max_email_count,
                    'max_retries': max_retries,
                    'email_folder': email_folder,
                    'imap_username': imap_username,
                    'imap_password': imap_password,
                    'imap_server': imap_server,
                    'imap_port': imap_port,
                    'smtp_username': smtp_username,
                    'smtp_password': smtp_password,
                    'smtp_server': smtp_server,
                    'smtp_port': smtp_port,
                    'max_threads': max_threads,
                    'heartbeat_interval': heartbeat_interval, 
                    'processed_uids_max_size': processed_uids_max_size, 
                    'max_results_memory_limit': max_results_memory_limit, 
                    'max_deleted_email_limit': max_deleted_email_limit
                })
                st.success("✅ 邮箱设置已保存")
                st.rerun()